package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToPersonalAnalysis.aibkAnalysisGCT


import android.annotation.SuppressLint
import android.graphics.drawable.Animatable
import android.net.Uri
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.drawee.controller.BaseControllerListener
import com.facebook.imagepipeline.image.ImageInfo
import com.lancoo.znbkxx.teacher.Ext
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ItemAnalysis
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ItemXAnalysis
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.SortIndexAnalysis
import com.lancoo.znbkxx.teacher.utils.HtmlUtils
import kotlinx.android.synthetic.main.item_gct_analysis.view.*

/**
 * Created by wealive on 2021/3/4.
 *
 *
 */
class AnalysisGctAdapter(private val isScanAnswer: Boolean, val genrelID: Int,
                         val stuAnswerList: String,
                         private val sortIndexList: List<SortIndexAnalysis>,
                         private val analysis: List<ItemXAnalysis>,
                         data: List<ItemAnalysis>) :
        BaseQuickAdapter<ItemAnalysis, BaseViewHolder>(data) {
    init {
        mLayoutResId = R.layout.item_gct_analysis
    }

    @SuppressLint("SetTextI18n")
    override fun convert(helper: BaseViewHolder, item: ItemAnalysis?) {
        val stuAnswer = stuAnswerList.split("、")[helper.layoutPosition].split("@|").let {
            if (it.size > 1) it[1] else it[0]
        }
        with(helper.itemView) {
            if (data[0].ImagePath.isNotEmpty()) {
                val controllerListener = Fresco.newDraweeControllerBuilder()
                        .setControllerListener(object :
                                BaseControllerListener<ImageInfo>() {
                            override fun onFinalImageSet(id: String?, imageInfo: ImageInfo?,
                                                         animatable: Animatable?) {
                                val params = sdv.layoutParams
                                params.height = imageInfo!!.height
                                params.width = imageInfo.width
                                sdv.layoutParams = params
                            }
                        })
                        .setUri(Uri.parse(Ext.OBSINFO?.ServerWithBucket + item?.ImagePath))
                        .build()
                if (genrelID == 48) {
                    if (helper.layoutPosition == 0) {
                        tv_total_answer.visibility = View.VISIBLE
                        tv_total_answer.text =
                                "${sortIndexList[0].SortIndex}整体作答."
                        sdv.visibility = View.VISIBLE
                        sdv.controller = controllerListener
                    } else {
                        tv_total_answer.visibility = View.GONE
                        sdv.visibility = View.GONE
                    }
                } else {
                    sdv.visibility = View.VISIBLE
                    sdv.controller = controllerListener
                }
                tv_stuAnswer.text = "${analysis[helper.layoutPosition].ItemSortIndex}."
            } else {
                tv_stuAnswer.text = "${analysis[helper.layoutPosition].ItemSortIndex}.$stuAnswer"
            }
            tv_answer.text =
                    HtmlUtils.setText(analysis[helper.layoutPosition].ItemAnswer.replace("$/", "或"))
            tv_analyze.text = HtmlUtils.setText(
                    analysis[helper.layoutPosition].ItemAnalysis.let { if (it.isBlank()) "...略" else it })
        }
    }
}